#include "pch.h"
#include "Particle.h"
   
namespace
{   
   DXROIDS_COM_PTR<ID2D1Geometry> s_particle_;

   void ReleaseStatics()
   {                  
      s_particle_.Release();
   }
}

void dxroids::Particle::Cleanup()
{
   s_particle_.Release();
}

 dxroids::Particle::Particle(Vector const & _pos, Vector const & _velocity, float _max_distance)
    : GameObject(_pos, 5.0f)
    , max_distance_(_max_distance) 
    , num_frames_(0)
    , max_frames_(static_cast<int>(ceilf(_max_distance / _velocity.Magnitude())))
 {
    SetVelocity(_velocity);
 } 

DXROIDS_COM_PTR<ID2D1Geometry> dxroids::Particle::CreateGeometry(DXROIDS_COM_PTR<ID2D1Factory> const & _factory,
   float _scale)
{   
   if (!s_particle_)
   {
      DXROIDS_COM_PTR<ID2D1EllipseGeometry> ellipse_geometry;
      D2D1_ELLIPSE ellipse = D2D1::Ellipse(D2D1::Point2F(), DXROIDS_PARTICLE_RADIUS, DXROIDS_PARTICLE_RADIUS);
      ThrowIfFailed(_factory->CreateEllipseGeometry(&ellipse, &ellipse_geometry));
      s_particle_ = ellipse_geometry;      
   }   
   return s_particle_;
}

void dxroids::Particle::UpdateFrame(
   GameTime const & _timer, 
   ClippingInformation const & _clip, 
   bool * _wrap_horizontal, 
   bool * _wrap_vertical)
{
   GameObject::UpdateFrame(_timer, _clip, _wrap_horizontal, _wrap_vertical);
   if (num_frames_++ >= max_frames_)
   {   
      SetIsActive(false);
      num_frames_ = 0;
   }
}
